/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package etf.pia.controlers;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import lombok.Getter;
import lombok.Setter;
import etf.pia.entity.*;
import etf.pia.util.Initializer;
import etf.pia.util.ServerUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Set;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.primefaces.model.UploadedFile;
import org.hibernate.criterion.Restrictions;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author Alejandro
 */
@Getter
@Setter
@ManagedBean(name = "courseBean")
@SessionScoped
public class CourseBean implements Serializable {

    @ManagedProperty("#{loginMediator.currUser.student}")
    private Student student;

    private Predmet predmet = null;
    private Katedra katedra = null;
    private byte[] file;

    private ArrayList<Obavestenja> infoList = null;

    private Spiskovi selectedSpisak;
    private ArrayList<LabVezbe> lab = null;
    private ArrayList<Spiskovi> spiskovi = null;
    private ArrayList<Domaci> domaci = null;
    private ArrayList<Fajlovi> predavanja = null;
    private ArrayList<Fajlovi> rokovi = null;
    private ArrayList<Fajlovi> vezbe = null;
    private ArrayList<Fajlovi> domaci_materijali = null;
    private ArrayList<Fajlovi> lab_materijali = null;

    public void prijaviSe() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            Prijava p = new Prijava();
            p.setFajl(file);
            p.setStudent(student);
            p.getSpiskovis().add(selectedSpisak);
            ses.save(p);
            t.commit();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void setSpisak(Spiskovi s) {
        selectedSpisak = s;
    }

    public String getPredmetById(int id) {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {

            predmet = getPById(id);
            katedra = predmet.getKatedra();
            Hibernate.initialize(katedra.getOpis());
            infoList = new ArrayList<Obavestenja>(predmet.getObavestenjas());

            lab = new ArrayList<LabVezbe>(predmet.getLabVezbes());

            domaci = new ArrayList<Domaci>(predmet.getDomacis());
            getFajlovi(new ArrayList<Fajlovi>(predmet.getFajlovis()));
            spiskovi = new ArrayList<Spiskovi>(predmet.getSpiskovis());
            t.commit();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
        return "coursePage?faces-redirect=true";
    }

    private Predmet getPById(int id) {
        for (Predmet p : new ArrayList<Predmet>(Initializer.toList(student))) {
            if (p.getIdPredmet() == id) {
                return p;
            }
        }
        return null;
    }

    public ArrayList<Fajlovi> toList(Set<Fajlovi> x) {
        return new ArrayList<Fajlovi>(x);
    }

    private void getFajlovi(ArrayList<Fajlovi> p) {
        predavanja = new ArrayList<Fajlovi>();
        vezbe = new ArrayList<Fajlovi>();
        rokovi = new ArrayList<Fajlovi>();
        for (Fajlovi m : p) {
            String tip = m.getTip();
            if (tip.equals("predavanja")) {
                predavanja.add(m);
            } else if (tip.equals("vezbe")) {
                vezbe.add(m);
            } else if (tip.equals("rokovi")) {
                rokovi.add(m);
            }
        }

    }
    
    public void handleFile(FileUploadEvent event) {
        file = event.getFile().getContents();
    }
}
